/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/

#include "stdafx.h"
#include "dll_utility.h"
#include "ochelper.h"

using namespace std;

HRESULT otlkcon_loadlibrary(wstring name)
{
    HRESULT result = S_OK;
    wstring tempName;
    HINSTANCE hinstStub = NULL;

    if( name.length() > 1 )
    {
        tempName = name;
    }
    else
    {
        tempName = OCHELPER_DEFAULT_DLL_NAME;
    }

    hinstStub = LoadLibraryW( tempName.c_str() );
    if( hinstStub == NULL )
    {
        ochelper_show_error();
        result = E_FAIL;
        goto load_end;
    }

load_end:
    return result;
}

HRESULT ochelper_show_error() 
{ 
    TCHAR szBuf[80]; 
    LPVOID lpMsgBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    wprintf( L"ERROR: %d: %s", dw, lpMsgBuf); 

    LocalFree(lpMsgBuf);
    
    return S_OK;
}